<template>
	<view class="pagesDoctorConsultation">
		<searchBox v-if="type==1" :currentCode='currentCode' @handleSearch='handleSearch'></searchBox>

		<!-- <view class="content" v-if='inquiryArr.length>0'>
			<view>
				<block v-for="(item,index) in inquiryArr" :key="index">
					<free-media-list v-if="loginUserId!=item.userId" :item="item" :index="index"
						@handleItem='getItem'></free-media-list>
				</block>
			</view>
		</view> -->
		<u-collapse :value="activeName" v-if='inquiryArr.length>0'>
			<u-collapse-item :title=" '订单ID ' + it.orderId + ' — ' + it.applyUser + ' — ' + getOrderType(it.orderType) "
				:name="idx" v-for="(it, idx) in inquiryArr">
				<block v-for="(item, inx) in it.messageInfoList" :key="inx">
					<free-media-list v-if="loginUserId!=item.userId" :item="item" :index="inx"
						@handleItem='getItem'></free-media-list>
				</block>
			</u-collapse-item>
		</u-collapse>

		<pagesNoData v-else></pagesNoData>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import freeMediaList from "@/components/chat/free-media-list.vue"
	import {
		updateIsRead,
		getUnReadCountList,
		findMessageList
	} from '@/api/platform/messageBoardController.js'
	import {
		getRoleList
	} from '@/api/platform/roles.js'
	import {
		getUserList
	} from '@/api/platform/users.js'
	export default {
		name: "doctorConsultation",
		props: ['orderId', 'type', 'currentCode'], // type 1 从医生端进去(有搜索框)   2 从病人端进去(无搜索框)
		components: {
			freeMediaList,
		},
		computed: {
			...mapState({
				loginUserId: state => state.user.userInfo.id,
			})
		},
		data() {
			return {
				messageBoardInfo: [],
				messageIdList: [],
				timerId: null,
				total: 0,
				roleList: [],
				filterForm: {
					name: "",
					orderId: '',
					phone: "",
					roleId: '',
					queryType: '', // 1 总的查询列表(废弃)  2根据订单id查询  3医生查询列表(根据消息查询沟通用户列表)   5 返回客服(和2,3数据返回格式不一样)  6 订单详情页面显示(类似2,但是待整理时有值)
				},
				inquiryArr: [],
				activeName: [0],
			};
		},

		mounted() {
			this.getRoleList()
			this.search()
		},
		methods: {
			handleSearch(data) {
				console.log('handleSearch---data', data)
				this.inquiryArr = []
				this.filterForm = data
				this.search()
			},
			getOrderType(orderType) {
				if (orderType == 1) {
					return '机构申请'
				} else {
					return '个人申请'
				}
			},
			getItem(item) {
				if (this.type == 1) {
					this.$store.commit('setDetailObj', item)
					let itemObj = item
					uni.navigateTo({
						url: `/subPackages/message/consoluting/consoluting?orderId=${itemObj.orderId}&selectUserId=${itemObj.userId}`
					})
				} else if (this.type == 2) {
					this.$store.commit('setDetailObj', item)
					let itemObj = item
					uni.navigateTo({
						url: `/subPackages/message/consoluting/consoluting?orderId=${this.orderId}&selectUserId=${itemObj.userId}`
					})
				}

			},
			// handleInput(num) {
			// 	if(num==1){
			// 		this.filterForm.orderId = uni.$u.trim(this.filterForm.orderId)
			// 	}else if(num==2){
			// 		this.filterForm.name = uni.$u.trim(this.filterForm.name)
			// 	}else if(num==3){
			// 		this.filterForm.phone = uni.$u.trim(this.filterForm.phone)
			// 	}
			// 	console.log('handleInput--')
			// 	this.search()
			// },
			search() {
				if (this.type == 1) {
					if (this.filterForm.name || this.filterForm.orderId || this.filterForm.phone) {
						this.filterForm.queryType = 2
						this.inquiryArr = []
						this.getUserList()

					} else {
						this.filterForm.queryType = 3
						this.inquiryArr = []
						this.getUserList()

					}
				} else if (this.type == 2) {
					this.filterForm.orderId = this.orderId
					this.filterForm.queryType = 6
					this.inquiryArr = []
					this.getUserList()
				}
			},

			getUserList() {
				getUserList(this.filterForm).then(res => {
					if (res.code == 200) {
						this.inquiryArr = res.data
						console.log('res.data--',res.data)
						if (res.index && res.index.length > 0) {
							this.activeName = res.index
						}else{
							this.activeName = [0]
						}
					}
				})
			},
			getRoleList() {
				getRoleList().then(res => {
					if (res.code == 200) {
						this.roleList = res.data
					}
				})
			},
			getUnReadCountList() {
				let query = {
					orderId: this.orderId
				}
				getUnReadCountList(query).then(res => {
					if (res.code == 200) {
						if (res.code === 200) {
							this.messageIdList = res.data.messageIdList || []
						}
					}
				})
			},
			findMessageList() {
				findMessageList(this.orderId).then(res => {
					if (res.code == 200) {
						this.messageBoardInfo = res.data.map(item => {
							return {
								...item,
								sendToUserName: this.reSetValue(item.messageSendToUserDetails)
							}
						})
					}
				})
			},
			reSetValue(data) {
				let finalValue = ''
				data.forEach((item, index) => {
					finalValue += (index == 0 ? '' : '、') + item.sendToUserName
				})
				return finalValue
			},
		}
	}
</script>

<style lang="scss" scoped>
	.pagesDoctorConsultation {

		// .searchBox {
		// 	::v-deep .u-form-item__body {
		// 		padding: 5rpx 0 !important;
		// 	}
		// }
		.content {
			padding: 20rpx 0;
		}
		::v-deep .u-collapse-item__content__text{
			padding: 12rpx 3px;
			// background-color: red;
		}

	}
</style>